package com.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.objects.Employee;


public class EmployeeDAOImpl implements EmployeeDAO{
	private Connection conn = null;

	@Override
	public Employee searchEmployee(String employee)  throws SQLException{
		
		
		DAOConnector daoConn = new DAOConnector();
		CallableStatement proc = null;
		Employee emp = new Employee();
		
		   try
		   {
			  conn  = daoConn.getConnection();
		      proc = conn.prepareCall("{ call GetEmployeeDetails(?) }");
		      proc.setInt(1, Integer.parseInt(employee));
		      
		      ResultSet rs = proc.executeQuery();
		      
		        while (rs.next())
		        {
		            emp.setEmployeeID(rs.getString("employee_Id"));
		            emp.setFirstName(rs.getString("first_name"));
		            emp.setLastName(rs.getString("last_name"));
		            emp.setEmailAddress(rs.getString("email_add"));
		            emp.setPositionCode(rs.getString("position_code"));
		            emp.setSeatCode(rs.getString("seat_Code"));
		            emp.setSeatTypeDesc(rs.getString("seat_Type_desc"));
		            emp.setLocalNumber(rs.getString("local_no"));
		           }
		        rs.close();
		   }
		   finally
		   {
		      try
		      {
		         proc.close();
		      }
		      catch (SQLException e) {}
		      conn.close();
		   }
		return emp;
	}

	@Override
	public boolean insertEmployee(Employee employee) {
		
		return false;
	}

	@Override
	public boolean updateEmployee(Employee employee) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteEmployee(String employee) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public List<Employee> searchAllEmployee() {
		DAOConnector daoConn = new DAOConnector();
		CallableStatement proc = null;
		List<Employee> emps =  new ArrayList<Employee>();
		Employee emp = new Employee();
		
		   try
		   {
			  conn  = daoConn.getConnection();
		      proc = conn.prepareCall("{ call GetAllEmployeeDetails() }");
		      		      
		      ResultSet rs = proc.executeQuery();
		      
		        while (rs.next())
		        {
		            emp.setEmployeeID(rs.getString("employee_Id"));
		            emp.setFirstName(rs.getString("first_name"));
		            emp.setLastName(rs.getString("last_name"));
		            emp.setEmailAddress(rs.getString("email_add"));
		            emp.setPositionCode(rs.getString("position_code"));
		            emp.setSeatCode(rs.getString("seat_Code"));
		            emp.setSeatTypeDesc(rs.getString("seat_Type_desc"));
		            emp.setLocalNumber(rs.getString("local_no"));
		            emps.add(emp);
		           }
		        rs.close();
		   
		   
		   }catch (SQLException e) {
               System.err.println("SQLException: " + e.getMessage());
		   }finally{
			   try{
			   if (proc != null) {	               
	                	proc.close();
	               
	            }
	            if (conn != null) {
	                  conn.close();
	            }
			   } catch (SQLException e) {
                   System.err.println("SQLException: " + e.getMessage());
   		   }
		  }
		return emps;
	}

}
